#include"LFMParser.h" 

#include"LFMCorrection.h"

#include<stdio.h>

LFMParser::LFMParser(){
	
};

QString LFMParser::parseToken(const char *xmlString){
	printf("LFMParser::parseToken()\nData: %s\n", xmlString);
	TiXmlDocument doc;
	const char *error = doc.Parse(xmlString);
	if( (error!=0) && doc.Error() ){
		printf("Error: doc.Parse(): %s\n", error);
		return QString();
	}
	TiXmlElement *rootElement = doc.RootElement();
	if(!rootElement){
		printf("Error: NULL rootElement\n");
		return QString();
	}
	if(rootElement->FirstAttribute() && rootElement->FirstAttribute()->Name() == QString("status")){
		printf("lfm status: %s\n", rootElement->FirstAttribute()->Value() );
	}else
		printf("Error: no lfm status found\n");
	for(TiXmlNode *node = rootElement->FirstChild(); node != 0; node = node->NextSibling()){
		if(node->Value() == QString("token")){
			if(node->ToElement())
				return QString( node->ToElement()->GetText() );
			node->Print(stdout, 10);
			printf("\n");
		}
	}
	return QString();
}

QString LFMParser::parseSession(const char *xmlString){
	TiXmlDocument doc;
	if(doc.Parse(xmlString) && doc.Error())
		return QString();
	TiXmlElement *rootElement = doc.RootElement();
	if(!rootElement)
		return QString();
	if(rootElement->FirstAttribute() && rootElement->FirstAttribute()->Name() == QString("status")){
		printf("lfm status: %s\n", rootElement->FirstAttribute()->Value() );
	}
	for(TiXmlNode *node = rootElement->FirstChild(); node != 0; node = node->NextSibling()){
		if(node->Value() == QString("session")){
			for(TiXmlNode *node2 = node->FirstChild(); node2 != 0; node2 = node2->NextSibling() ){
				if(node2->Value() == QString("key") && node2->ToElement())
					return QString( node2->ToElement()->GetText() );
				node2->Print(stdout, 10);
				printf("\n");
			}
		}
	}
	return QString();
}

LFMCorrection* LFMParser::parseCorrection(const char *xmlString){
	TiXmlDocument doc;
	if(doc.Parse(xmlString) && doc.Error())
		return 0;
	TiXmlElement *rootElement = doc.RootElement();
	if(!rootElement || !rootElement->FirstChild() || !rootElement->FirstChild()->ToElement())
		return 0;
	rootElement = rootElement->FirstChild()->ToElement();
	if(rootElement->Value() == QString("scrobbles")){//Might return multiple corrections
		LFMCorrection *ret = 0;
		LFMCorrection *current = 0;
		for(TiXmlNode *node = rootElement->FirstChild(); node != 0; node = node->NextSibling()){
			if(node->Value() == QString("scrobble")){
				LFMCorrection *val = parseCorrection(node->ToElement());
				if(val){
					if(ret == 0){
						ret = val;
						current = val;
					}else{
						current->next = val;
						current = val;
					}
				}
			}
		}
	}else if(rootElement->Value() == QString("nowplaying")){//At most one element to return
		return parseCorrection(rootElement);
	}else
		printf("LFMParser::parseCorrection, unknown rootElement->Value(): %s\n", rootElement->Value());
	return 0;
}

LFMCorrection* LFMParser::parseCorrection(TiXmlElement *element){
	bool hasCorrections = false;
	for(TiXmlNode *node = element->FirstChild(); !hasCorrections && node != 0; node = node->NextSibling())
		hasCorrections = node->ToElement() && node->ToElement()->Attribute("corrected") == QString("1");
	if(!hasCorrections)
		return 0;
	LFMCorrection *ret = new LFMCorrection;
	for(TiXmlNode *node = element->FirstChild(); node != 0; node = node->NextSibling()){
		TiXmlElement *e = node->ToElement();
		if(e==0)
			continue;
		if(e->Value() == QString("track")){
			ret->title = QString::fromUtf8(e->GetText());
			ret->titleCorrected = e->Attribute("corrected") == QString("1");
		}else if(e->Value() == QString("artist")){
			ret->artist = QString::fromUtf8(e->GetText());
			ret->artistCorrected = e->Attribute("corrected") == QString("1");
		}else if(e->Value() == QString("album")){
			ret->album = QString::fromUtf8(e->GetText());
			ret->albumCorrected = e->Attribute("corrected") == QString("1");
		}
	}
	return ret;
}